/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package excel;

import aaa.Configurator;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import javax.swing.filechooser.FileSystemView;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.TermEnum;

/**
 *
 * @author kurpicz
 */
public class CSVFile {
    
    private IndexReader ir;
    
        public CSVFile(IndexReader ir){
            this.ir = ir;
        }
        
        public void createCSVFile(String filename) throws IOException {
            FileSystemView filesys = FileSystemView.getFileSystemView();
            //String CSVFileName = filesys.getHomeDirectory().getPath() + "//" + filename + ".csv";
            String CSVFileName = Configurator.getConfigProperty("xls_path")+"//"+filename+".csv"; 
            TermEnum terms = ir.terms();
            String content = ""; 
            while (terms.next()) {
                content = content + terms.term().text() + ";" + Integer.toString(terms.docFreq()) + "\n";
            }
            FileWriter fstream;
            try {
                fstream = new FileWriter(CSVFileName);
            }
            catch (IOException ex1){
                File f = new File(filename+".csv");
                f.createNewFile();
                fstream = new FileWriter(f);
            }
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(content);
            //Close the output stream
            out.close();            
        }
    
}
